// no conflicts with other frameworks
var $j = jQuery.noConflict();

//
// window.onload function to setup various javascripts
//


/*****************************************************************************************
 *
 *                           General helper functions
 *
 *****************************************************************************************/

/**
 * Extend the String object so that we can use the trim() function like; sVar.trim();
 *
 *     var first_name = "Chris ";
 *     first_name = first_name.trim();
 *
 * @return string
 */
String.prototype.trim = function() {
  var a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
};

/**
 * Write an email address to the screen. Helps so that bots can't parse email addresses from the site.
 *
 *     writeEmail("info", "info", "churchmedia.cc");
 *
 * @param string
 * @param string
 * @param string
 */
function writeEmail(contact, email, emailHost) {
  document.write("<a href=" + "&#109a&#105l" + "&#116&#111:" + email + "@" + emailHost+ ">" + contact + "@" + emailHost+"</a>");
}

/**
 * Pop up window in a certain size and no scrollbar.
 */
function pop(url, name, props) {
	window.open(url, name, props);
}




/*****************************************************************************************
 *
 *                    Functions for forms - highlight error fields
 *
 *****************************************************************************************/
function removeErrorClass(control_id) {
  $j('#'+control_id).parent().parent().removeClass('error');
  return true;
}
function addError(control_id) {
  $j('#'+control_id).select().focus();
  $j('#'+control_id).parent().parent().addClass('error');
  return true;
}




/*****************************************************************************************
 *
 *                             Functions for form validation
 *
 *****************************************************************************************/

// Validate Contact Us Form
function validate_contact_us_form() {
  //Form validation
  var bError = false;
  if ($j('#cont_first_name').val() == '') { addError('cont_first_name'); bError = true; } else { removeErrorClass('cont_first_name'); }
  if ($j('#cont_last_name').val() == '') { addError('cont_last_name'); bError = true; } else { removeErrorClass('cont_last_name'); }
  if ($j('#cont_address_1').val() == '') { addError('cont_address_1'); bError = true; } else { removeErrorClass('cont_address_1'); }
  if ($j('#cont_city').val() == '') { addError('cont_city'); bError = true; } else { removeErrorClass('cont_city'); }
  if ($j('#cont_state').val() == '') { addError('cont_state'); bError = true; } else { removeErrorClass('cont_state'); }
  if ($j('#cont_zip').val() == '') { addError('cont_zip'); bError = true; } else { removeErrorClass('cont_zip'); }
  if ($j('#cont_country').val() == '') { addError('cont_country'); bError = true; } else { removeErrorClass('cont_country'); }
  if ($j('#cont_phone').val() == '') { addError('cont_phone'); bError = true; } else { removeErrorClass('cont_phone'); }

  var sEmail = $j('#cont_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('cont_email'); bError = true; } else { removeErrorClass('cont_email'); }

  if ($j('#cont_comments').val() == '') { addError('cont_comments'); bError = true; } else { removeErrorClass('cont_comments'); }
  if ($j('#cont_security_code').val() == '') { addError('cont_security_code'); bError = true; } else { removeErrorClass('cont_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

// Validate Contact Us Form
function validate_wedding_registration_form() {
  //Form validation
  var bError = false;
  if ($j('#wedding_date_1').val() == '') { addError('wedding_date_1'); bError = true; } else { removeErrorClass('wedding_date_1'); }
  if ($j('#wedding_date_2').val() == '') { addError('wedding_date_2'); bError = true; } else { removeErrorClass('wedding_date_2'); }
  if ($j('#wedding_date_3').val() == '') { addError('wedding_date_3'); bError = true; } else { removeErrorClass('wedding_date_3'); }
  if ($j('#bride_name').val() == '') { addError('bride_name'); bError = true; } else { removeErrorClass('bride_name'); }
  if ($j('#bride_phone').val() == '') { addError('bride_phone'); bError = true; } else { removeErrorClass('bride_phone'); }
  if ($j('#groom_name').val() == '') { addError('groom_name'); bError = true; } else { removeErrorClass('groom_name'); }
  if ($j('#groom_phone').val() == '') { addError('groom_phone'); bError = true; } else { removeErrorClass('groom_phone'); }

  var sEmail = $j('#bride_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('bride_email'); bError = true; } else { removeErrorClass('bride_email'); }
  
  var sEmail = $j('#groom_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('groom_email'); bError = true; } else { removeErrorClass('groom_email'); }

  if ($j('#security_code').val() == '') { addError('security_code'); bError = true; } else { removeErrorClass('security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

// Validate Lead a group Form
function validate_lead_a_group_form() {
  //Form validation
  var bError = false;
  if ($j('#first_name').val() == '') { addError('first_name'); bError = true; } else { removeErrorClass('first_name'); }
  if ($j('#last_name').val() == '') { addError('last_name'); bError = true; } else { removeErrorClass('last_name'); }
  if ($j('#address_1').val() == '') { addError('address_1'); bError = true; } else { removeErrorClass('address_1'); }
  if ($j('#city').val() == '') { addError('city'); bError = true; } else { removeErrorClass('city'); }
  if ($j('#state').val() == '') { addError('state'); bError = true; } else { removeErrorClass('state'); }
  if ($j('#zip').val() == '') { addError('zip'); bError = true; } else { removeErrorClass('zip'); }
  if ($j('#phone').val() == '') { addError('phone'); bError = true; } else { removeErrorClass('phone'); }
  if ($j('#group_type').val() == '') { addError('group_type'); bError = true; } else { removeErrorClass('group_type'); }

  var sEmail = $j('#email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('email'); bError = true; } else { removeErrorClass('email'); }

//  if ($j('#comments').val() == '') { addError('comments'); bError = true; } else { removeErrorClass('comments'); }
  if ($j('#lead_security_code').val() == '') { addError('lead_security_code'); bError = true; } else { removeErrorClass('lead_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

// Validate Register Group Form
function validate_register_group_form() {
  //Form validation
  var bError = false;
  if ($j('#group_id').val() == '') { addError('group_id'); bError = true; } else { removeErrorClass('group_id'); }
  if ($j('#first_name').val() == '') { addError('first_name'); bError = true; } else { removeErrorClass('first_name'); }
  if ($j('#last_name').val() == '') { addError('last_name'); bError = true; } else { removeErrorClass('last_name'); }
  if ($j('#address_1').val() == '') { addError('address_1'); bError = true; } else { removeErrorClass('address_1'); }
  if ($j('#city').val() == '') { addError('city'); bError = true; } else { removeErrorClass('city'); }
  if ($j('#state').val() == '') { addError('state'); bError = true; } else { removeErrorClass('state'); }
  if ($j('#zip').val() == '') { addError('zip'); bError = true; } else { removeErrorClass('zip'); }
  if ($j('#country').val() == '') { addError('country'); bError = true; } else { removeErrorClass('country'); }
  if ($j('#phone').val() == '') { addError('phone'); bError = true; } else { removeErrorClass('phone'); }

  var sEmail = $j('#email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('email'); bError = true; } else { removeErrorClass('email'); }

//  if ($j('#comments').val() == '') { addError('comments'); bError = true; } else { removeErrorClass('comments'); }
  if ($j('#register_group_security_code').val() == '') { addError('register_group_security_code'); bError = true; } else { removeErrorClass('register_group_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

// Validate Enewsletter Form
function validate_enewsletter_form() {
  //Form validation
  var bError = false;
  if ($j('#enews_first_name').val() == '') { addError('enews_first_name'); bError = true; } else { removeErrorClass('enews_first_name'); }
  if ($j('#enews_last_name').val() == '') { addError('enews_last_name'); bError = true; } else { removeErrorClass('enews_last_name'); }
  if ($j('#enews_zip').val() == '') { addError('enews_zip'); bError = true; } else { removeErrorClass('enews_zip'); }
  
  var sEmail = $j('#enews_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('enews_email'); bError = true; } else { removeErrorClass('enews_email'); }

  if ($j('#enews_security_code').val() == '') { addError('enews_security_code'); bError = true; } else { removeErrorClass('enews_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

// Validate Send 2 Friend form
function validate_send_to_friend_form() {
  //Form validation
  var bError = false;
  if ($j('#stf_name').val() == '') { addError('stf_name'); bError = true; } else { removeErrorClass('stf_name'); }
  var sEmail = $j('#stf_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('stf_email'); bError = true; } else { removeErrorClass('stf_email'); }
  if ($j('#stf_friend_name_1').val() == '') { addError('stf_friend_name_1'); bError = true; } else { removeErrorClass('stf_friend_name_1'); }
  sEmail = $j('#stf_friend_email_1').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('stf_friend_email_1'); bError = true; } else { removeErrorClass('stf_friend_email_1'); }
  if ($j('#stf_friend_name_2').val() !== "") {
    sEmail = $j('#stf_friend_email_2').val();
    if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('stf_friend_email_2'); bError = true; } else { removeErrorClass('stf_friend_email_2'); }
  }

  if ($j('#stf_security_code').val() == '') { addError('stf_security_code'); bError = true; } else { removeErrorClass('stf_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}


// Validate Baptism Form
function validate_baptism_form() {
  //Form validation
  var bError = false;
  if ($j('#first_name').val() == '') { addError('first_name'); bError = true; } else { removeErrorClass('first_name'); }
  if ($j('#last_name').val() == '') { addError('last_name'); bError = true; } else { removeErrorClass('last_name'); }

  var sEmail = $j('#email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('email'); bError = true; } else { removeErrorClass('email'); }

  if ($j('#birthdate').val() == '') { addError('birthdate'); bError = true; } else { removeErrorClass('birthdate'); }
  if ($j('#when_how_got_saved').val() == '') { addError('when_how_got_saved'); bError = true; } else { removeErrorClass('when_how_got_saved'); }
  if ($j('#why_get_baptized').val() == '') { addError('why_get_baptized'); bError = true; } else { removeErrorClass('why_get_baptized'); }

  if ($j('#security_code').val() == '') { addError('security_code'); bError = true; } else { removeErrorClass('security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

// Validate Baby Dedication Form
function validate_baby_dedication_form() {
  //Form validation
  var bError = false;
  if ($j('#parents_names').val() == '') { addError('parents_names'); bError = true; } else { removeErrorClass('parents_names'); }
  if ($j('#child_name').val() == '') { addError('child_name'); bError = true; } else { removeErrorClass('child_name'); }

  var sEmail = $j('#email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('email'); bError = true; } else { removeErrorClass('email'); }

  if ($j('#birthdate').val() == '') { addError('birthdate'); bError = true; } else { removeErrorClass('birthdate'); }

  if ($j('#security_code').val() == '') { addError('security_code'); bError = true; } else { removeErrorClass('security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}


// Validate Prayer Request Form
function validate_prayer_request_form() {
  //Form validation
  var bError = false;
  if ($j('#first_name').val() == '') { addError('first_name'); bError = true; } else { removeErrorClass('first_name'); }
  if ($j('#last_name').val() == '') { addError('last_name'); bError = true; } else { removeErrorClass('last_name'); }

  var sEmail = $j('#email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('email'); bError = true; } else { removeErrorClass('email'); }

  if ($j('#prayer_request').val() == '') { addError('prayer_request'); bError = true; } else { removeErrorClass('prayer_request'); }
  if ($j('#security_code').val() == '') { addError('security_code'); bError = true; } else { removeErrorClass('security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}